package cn.math.appmath.action;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.struts2.ServletActionContext;

import cn.math.appmath.bean.MemberBean;
import cn.math.appmath.biz.MemberBiz;
import cn.math.appmath.entity.Member;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class MemberAction extends ActionSupport {
	private static final long serialVersionUID = -4565898601842133546L;
	MemberBiz memberBiz = null;
	List<Member> list = null;
	private MemberBean memberBean;
	private Member member;
	private String message;
	private Integer[] nos;
	
	//上传文件的files对象
	private File file;
	//上传文件名
	private String fileFileName;
	//上传文件的MIME类型
	private String fileContentType;
	//保存上传文件的目录，相对于Web应用程序的根路径，在struts.xml文件中配置
	private String uploadDir;
	
	public void setMemberBiz(MemberBiz memberBiz) {
		this.memberBiz = memberBiz;
	}

	public String findByField() throws Exception{
		ActionContext ac = ActionContext.getContext();
		Map session = ac.getSession();
		list = memberBiz.findByField("微分动力系统");
		if(list != null){
			session.put("member1", list);
		}
		list = memberBiz.findByField("应用代数学");
		if(list != null){
			session.put("member2", list);
		}
		list = memberBiz.findByField("应用软件");
		if(list != null){
			session.put("member3", list);
		}
		return SUCCESS;
	}
	public String findById() throws Exception{
		Integer param = new Integer(ServletActionContext.getRequest().getParameter("id"));
		member = memberBiz.getById(param);
		return SUCCESS;
	}
	
	public String memberforward(){
		message = "";
		return SUCCESS;
	}
	
	/**
	 * 保存成员信息
	 * @return
	 */
	public String saveMember(){
		if(file != null){
			String newImageName = uploadImage();
			if(newImageName != null && !newImageName.equals("")){
				member.setMphoto(uploadDir + "/" + newImageName);
			}
		}
		if(memberBiz.save(member)){
			message = "保存成员成功";
			return SUCCESS;
		}
		message="保存成员失败";
		return ERROR;
	}

	/**
	 * 图片上传处理方法 
	 * @return
	 */
	public String uploadImage(){
		try{
			String newImageName = null;
			long now = new Date().getTime();
			
			//得到保存上传文件的目录的真实路径
			String path = ServletActionContext.getServletContext().getRealPath(uploadDir);
			
			File dir = new File(path);
			//如果这个目录不存在，则创建它
			if(!dir.exists())
				dir.mkdir();
			int index = fileFileName.lastIndexOf('.');
			//判断上传文件名是否有扩展名，以时间作为新的文件名
			if(index != -1)
				newImageName = now + fileFileName.substring(index);
			else
				newImageName = Long.toString(now);
			BufferedOutputStream bos = null;
			BufferedInputStream bis = null;
			
			//读取保存在临时目录下的上传文件、写入到新的文件中
			try{
				FileInputStream fis = new FileInputStream(file);
				bis = new BufferedInputStream(fis);
				
				FileOutputStream fos = new FileOutputStream(new File(dir, newImageName));
				bos = new BufferedOutputStream(fos);
				
				byte[] buf = new byte[4096];
				
				int len = -1;
				while((len = bis.read(buf)) != -1)
					bos.write(buf, 0, len);				
			}catch(IOException e){
				e.printStackTrace();
			}finally{
				try{
					if(null != bis)
						bis.close();
				}catch(IOException e){
					e.printStackTrace();
				}
				
				try{
					if(null != bos)
						bos.close();
				}catch(IOException e){
					e.printStackTrace();
				}
			}
			return newImageName;
		}catch(Exception ex){
			ex.printStackTrace();
			return null;
		}
	}
	
	public String backQueryMember() {
		String pageStr = ServletActionContext.getRequest().getParameter("page");
		int page = 0;
		if(pageStr != null && !"".equals(pageStr))
			page = Integer.parseInt(pageStr);
		String param = ServletActionContext.getRequest().getParameter("param");
		if(param == null || "".equals(param))
			param = "backward";
		memberBean = memberBiz.findAll(page, param);
		return SUCCESS;
	}
	
	/**
	 * 删除学科成员
	 * @return
	 */
	public String deleteMember() {
		String noStr = ServletActionContext.getRequest().getParameter("no");
		if(noStr != null && !"".equals(noStr)){
			Integer no = new Integer(noStr);
			nos = new Integer[]{no};
		}
		if(this.memberBiz.delete(nos)){
			return SUCCESS;
		}else{
			message = "删除学科成员信息失败";
			return INPUT;
		}
	}
	
	public Member getMember() {
		return member;
	}

	public void setMember(Member member) {
		this.member = member;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public File getFile() {
		return file;
	}

	public void setFile(File file) {
		this.file = file;
	}

	public String getFileFileName() {
		return fileFileName;
	}

	public void setFileFileName(String fileFileName) {
		this.fileFileName = fileFileName;
	}

	public String getFileContentType() {
		return fileContentType;
	}

	public void setFileContentType(String fileContentType) {
		this.fileContentType = fileContentType;
	}

	public String getUploadDir() {
		return uploadDir;
	}

	public void setUploadDir(String uploadDir) {
		this.uploadDir = uploadDir;
	}

	public MemberBean getMemberBean() {
		return memberBean;
	}

	public void setMemberBean(MemberBean memberBean) {
		this.memberBean = memberBean;
	}

	public Integer[] getNos() {
		return nos;
	}

	public void setNos(Integer[] nos) {
		this.nos = nos;
	}
	
}
